"""
新闻相关的 Pydantic 模式
"""
from typing import Optional
from pydantic import BaseModel, Field
from datetime import datetime


class NewsBase(BaseModel):
    """新闻基础模式"""
    title: str = Field(..., max_length=200, description="新闻标题")
    excerpt: Optional[str] = Field(None, max_length=500, description="新闻摘要")
    content: str = Field(..., description="新闻内容（富文本）")
    cover_image: Optional[str] = Field(None, max_length=500, description="封面图片URL")
    category: str = Field(default="公司新闻", max_length=50, description="新闻分类")
    author: str = Field(default="管理员", max_length=100, description="作者")
    is_published: bool = Field(default=True, description="是否发布")
    is_featured: bool = Field(default=False, description="是否置顶")


class NewsCreate(NewsBase):
    """创建新闻请求模式"""
    class Config:
        schema_extra = {
            "example": {
                "title": "公司荣获环保技术创新奖",
                "excerpt": "我公司在环保技术创新方面取得重大突破",
                "content": "<p>详细的新闻内容...</p>",
                "cover_image": "/images/news1.jpg",
                "category": "公司新闻",
                "author": "管理员",
                "is_published": True,
                "is_featured": False
            }
        }


class NewsUpdate(BaseModel):
    """更新新闻请求模式"""
    title: Optional[str] = Field(None, max_length=200, description="新闻标题")
    excerpt: Optional[str] = Field(None, max_length=500, description="新闻摘要")
    content: Optional[str] = Field(None, description="新闻内容（富文本）")
    cover_image: Optional[str] = Field(None, max_length=500, description="封面图片URL")
    category: Optional[str] = Field(None, max_length=50, description="新闻分类")
    author: Optional[str] = Field(None, max_length=100, description="作者")
    is_published: Optional[bool] = Field(None, description="是否发布")
    is_featured: Optional[bool] = Field(None, description="是否置顶")


class NewsResponse(NewsBase):
    """新闻响应模式"""
    id: int
    view_count: int
    published_at: datetime
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True


class NewsListResponse(BaseModel):
    """新闻列表响应模式"""
    items: list[NewsResponse]
    total: int
    page: int
    size: int
    pages: int


class NewsSearchParams(BaseModel):
    """新闻搜索参数"""
    keyword: Optional[str] = Field(None, description="搜索关键词")
    category: Optional[str] = Field(None, description="新闻分类")
    published_only: bool = Field(default=True, description="只显示已发布的新闻")
    featured_only: bool = Field(default=False, description="只显示置顶新闻")